/*****************************************************************************
 * hal_bk72_gpio.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

#include "plum_api.h"
#include "hal/source/hal_bk72/hal_bk72.h"

/*****************************************************************************
 * Included Files
 ****************************************************************************/

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_u32 id;
    plum_void (*func)(plum_u32 id);
    struct list_head list;
} bk72_gpio_irq_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE bk72_gpio_irq_t gpio_head;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_u32 hal_bk72_gpio_pin(plum_u32 id)
{
    return (PLUM_HAL_GPIO_PIN(id));
}

PLUM_PRIVATE
plum_u32 hal_bk72_gpio_dir(plum_u32 id, plum_hal_gpio_com_cof_t com)
{
    plum_u32 rc = PLUM_ECODE_OK;

    bk_gpio_t idx = hal_bk72_gpio_pin(id);

    bk_gpio_config_t mode;
    rc = BkGpioInitialize(idx, mode);
    if (rc) {
        LOG_E("BkGpioInitialize err,rc");
    }

    return (rc);
}

PLUM_PRIVATE
bk72_gpio_irq_t *hal_bk72_gpio_irq_apply(plum_u32 id,
                                         plum_void (*callback)(plum_u32 id))
{
    bk72_gpio_irq_t *node = plum_null;

    do {
        node = (bk72_gpio_irq_t *)plum_hal_malloc(sizeof(bk72_gpio_irq_t));
        if (!node) {
            break;
        }

        memset(node, 0, sizeof(bk72_gpio_irq_t));
        node->func = callback;
        node->id   = id;
    } while (0);

    return (node);
}

PLUM_PRIVATE
bk72_gpio_irq_t *hal_bk72_gpio_irq_find(plum_u32 id)
{
    bk72_gpio_irq_t *node = plum_null;

    struct list_head *pos;
    list_for_each(pos, &gpio_irq_head.list)
    {
        node = list_entry(pos, bk72_gpio_irq_t, list);
        if (node->id == id) {
            break;
        }
    }

    return (node);
}

PLUM_PRIVATE
bk72_gpio_irq_t *hal_bk72_gpio_irq_find_by_idx(plum_u32 idx)
{
    bk72_gpio_irq_t *node = plum_null;

    struct list_head *pos;
    list_for_each(pos, &gpio_irq_head.list)
    {
        node = list_entry(pos, bk72_gpio_irq_t, list);
        if (PLUM_HAL_UART_IDX(node->id) == idx) {
            break;
        }
    }

    return (node);
}

PLUM_PRIVATE
plum_s32 hal_bk72_gpio_irq_insert(bk72_gpio_irq_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_add(&node->list, &gpio_irq_head.list);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 hal_hi_gpio_irq_del(bk72_gpio_irq_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_del(&node->list);
    } while (0);

    return (rc);
}

plum_void hal_hi_gpio_irq_handle(plum_void *arg)
{
    plum_u32 idx = *(plum_u32 *)arg;

    bk72_gpio_irq_t *node = hal_bk72_gpio_irq_find_by_idx(idx);
    if (!node) {
        LOG_E("not find the irq,id:%d", node->id);
        return;
    }
    else {
        LOG_D("catch gpio irq,id:%d", node->id);
        node->func(node->id);
    }
}

PLUM_PRIVATE
bk_gpio_irq_trigger_t hal_bk72_gpio_trip(plum_hal_gpio_irq_t t)
{
    bk_gpio_irq_trigger_t trigger = IRQ_TRIGGER_LOW_LEVEL;

    switch (t) {
        case PLUM_HAL_GPIO_IRQ_FALLING_EDGE:
            trigger = IRQ_TRIGGER_FALLING_EDGE : break;
        case PLUM_HAL_GPIO_IRQ_RISING_EDGE:
            trigger = IRQ_TRIGGER_RISING_EDGE : break;
        case PLUM_HAL_GPIO_IRQ_HIGH_LEVEL:
            trigger = IRQ_TRIGGER_HGIH_LEVEL : break;
        case PLUM_HAL_GPIO_IRQ_LOW_LEVEL:
            trigger = IRQ_TRIGGER_LOW_LEVEL : break;
        default:
            break;
    }

    return (trigger);
}

PLUM_PRIVATE
plum_s32 hal_bk72_gpio_irq(plum_u32 id, plum_hal_gpio_irq_cof_t irq)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_GPIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        bk72_gpio_irq_t *node = hal_bk72_gpio_irq_apply(id, irq.irq_handle);
        if (!node) {
            LOG_E("hal_bk72_gpio_irq_apply err");
            rc = PLUM_ECODE_EMEM;
            break;
        }

        bk_gpio_t             idx  = hal_bk72_gpio_pin(id);
        bk_gpio_irq_trigger_t trip = hal_bk72_gpio_trip(irq.trip);

        BkGpioEnableIRQ(idx, trip, hal_hi_gpio_irq_handle, &node->id);

        hal_bk72_gpio_irq_insert(node);
    } while (0);

    return (rc);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_hal_gpio_init(plum_u32 id, plum_hal_gpio_cof_t *cof)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_GPIO(id) || !cof) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (cof->mode == PLUM_HAL_GPIO_MODE_COMMON) {
            rc = hal_bk72_gpio_dir(id, cof->config.com);
        }
        else if (cof->mode == PLUM_HAL_GPIO_MODE_IRQ) {
            rc = hal_bk72_gpio_irq(id, cof->config.irq);
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_bit plum_hal_gpio_read(plum_u32 id)
{
    plum_bit rc = plum_false;

    do {
        if (!PLUM_HAL_ID_IS_GPIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        bk_gpio_t idx = hal_bk72_gpio_pin(id);
        rc            = BkGpioInputGet() ? plum_true : plum_false;
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_gpio_write(plum_u32 id, plum_hal_gpio_sta_t sta)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_GPIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        bk_gpio_t idx = hal_bk72_gpio_pin(id);
        if (sta == PLUM_HAL_GPIO_LOW) {
            BkGpioOutputLow(idx);
        }
        else if (sta == PLUM_HAL_GPIO_HIGH) {
            BkGpioOutputHigh(idx);
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_hal_gpio_irq_enable(plum_u32 id)
{
}

PLUM_PUBLIC
plum_void plum_hal_gpio_irq_disable(plum_u32 id)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_GPIO(id)) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        bk_gpio_t idx = hal_bk72_gpio_pin(id);

        BkGpioDisableIRQ(idx);
    } while (0);

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
